<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>所有分类-azhuo商城</title>
	<meta name="renderer" content="webkit|ie-comp|ie-stand">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

	<link rel="shortcut icon" href="${pageContext.request.contextPath }/image/logo/xyz.ico" type="image/x-icon" />
	<link rel="stylesheet" href="${pageContext.request.contextPath }/css/font.css">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/css/xadmin.css">

	<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
	<script src="${pageContext.request.contextPath }/lib/layui/layui.js" charset="utf-8"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/xadmin.js"></script>

	<script>
		layui.use(['form'], function () {
			form = layui.form;

		});



		// 单条删除
		function member_del(obj, id) {
			layer.confirm('确认要删除吗？', function (index) {
				//发异步删除数据

				$.ajax({
					type: "POST",// 请求的方式默认为get 改为Post
					url: "${pageContext.request.contextPath}/admin/AdminCategoryServlet",// 往服务器端请求的地址 写成绝对路径
					data: {
						method: "removeCategory",
						cid: id
					},// 往服务器端发送的数据
					// 接受服务器端数据函数 回调函数
					success: function (data) {
						// data是服务器返回的数据
						if (data) {
							console.log(data);
							$(obj).parents("tr").remove();
							layer.msg('已删除!', { icon: 1, time: 1000 });
						}
					}
				});
			});
		}


		// 批量删除
		function delAll(argument) {

			console.log("argument = " + argument);

			var data = tableCheck.getData();

			layer.confirm('确认要“' + data + '”删除分类吗？', function (index) {

				console.log("data = " + data);

				//捉到所有被选中的，发异步进行删除
				$.ajax({
					type: "POST",// 请求的方式默认为get 改为Post
					url: "${pageContext.request.contextPath}/admin/AdminCategoryServlet",// 往服务器端请求的地址 写成绝对路径
					data: {
						method: "removeAllCategory",
						cnames: data + ""
					},// 往服务器端发送的数据
					// 接受服务器端数据函数 回调函数
					success: function (data) {
						// data是服务器返回的数据
						if (data) {
							// 在数据库中删除成功，在页面中进行删除
							layer.msg('删除成功', { icon: 1 });
							$(".layui-form-checked").not('.header').parents('tr').remove();
						}
					}
				});
				
			});
		}
	</script>
</head>

<body>
	<div class="x-nav">
		<span class="layui-breadcrumb">
			<a href="${pageContext.request.contextPath}/adminjsps/home.jsp" target="iframe_a">首页</a>
			<a><cite>分类管理</cite></a>
		</span>
		<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
			href="javascript:location.replace(location.href);" title="刷新">
			<i class="layui-icon" style="line-height:30px">ဂ</i></a>
	</div>
	<div class="x-body">
		<div class="layui-row">
			<form class="layui-form layui-col-md12 x-so layui-form-pane" method="post"
				action="${pageContext.request.contextPath }/admin/AdminCategoryServlet">
				<input type="hidden" name="method" value="addCategory" />
				<input class="layui-input" placeholder="请输入新增分类名称" name="cname" /><span style="color:red;">${msg}</span>
				<input class="layui-input" placeholder="请输入分类描述" name="cdesc" />
				<button class="layui-btn" lay-submit="" lay-filter="sreach">
					<i class="layui-icon"></i>增加
				</button>
			</form>
		</div>
		<xblock>
			<button class="layui-btn layui-btn-danger" onclick="delAll()">
				<i class="layui-icon"></i>批量删除
			</button>
			<span class="x-right" style="line-height:40px">共有数据：${fn:length(categoryList) } 条</span> </xblock>
		<table class="layui-table layui-form" style="text-align: center;">
			<thead>
				<tr style="text-align: center;">
					<th width="20">
						<div class="layui-unselect header layui-form-checkbox" lay-skin="primary"
							style="text-align: center;">
							<i class="layui-icon">&#xe605;</i>
						</div>
					</th>
					<th style="width: 30px;text-align: center;">序号</th>
					<th style="width: 200px;text-align: center;">分类名称</th>
					<th style="width: 200px;text-align: center;">分类描述</th>
					<th style="width: 200px;text-align: center;">操作</th>
			</thead>
			<tbody class="x-cate">
				<%-- 输出所有分类 --%>
				<c:forEach items="${categoryList }" var="category" varStatus="status">
					<tr cate-id='1' fid='0'>
						<td>
							<div class="layui-unselect layui-form-checkbox" lay-skin="primary"
								data-id='${category.cname }'>
								<i class="layui-icon">&#xe605;</i>
							</div>
						</td>
						<td>${status.count }</td>
						<td>${category.cname }</td>
						<td>${category.cdesc }</td>
						<td class="td-manage">
							<button class="layui-btn layui-btn layui-btn-xs"
								onclick="x_admin_show('修改分类','${pageContext.request.contextPath}/admin/AdminCategoryServlet?method=loadCategoryById&cid=${category.cid }')">
								<i class="layui-icon">&#xe642;</i>编辑
							</button>
							<button class="layui-btn-danger layui-btn layui-btn-xs"
								onclick="member_del(this,'${category.cid }')" href="javascript:;">
								<i class="layui-icon">&#xe640;</i>删除
							</button>
						</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>
	</div>
</body>

</html>